package com.dx.mes.service.query.service;


import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IFipinqAnnService;
import com.dx.mes.facade.query.service.IFipinqboxService;
import com.dx.mes.facade.query.tx.FipinqAnn.FipinqannI;
import com.dx.mes.facade.query.tx.FipinqAnn.FipinqannO;
import com.dx.mes.facade.query.tx.FipinqAnn.FipinqannOA;
import com.dx.mes.facade.query.tx.Fipinqbox.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("fipinqannService")
public class FipinqAnnService implements IFipinqAnnService{
	private LogUtils logUtils;
	private static final String TX_NAME = "FIPINQANN";
	
	@Autowired
	private IAnnouncementDao announcementDao;

	@Transactional
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FipinqAnnService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FipinqAnnService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		Session session = announcementDao.getCrSession();
		String outMsg = "";
		
		FipinqannI inTrx = new FipinqannI();
		FipinqannO outTrx = new FipinqannO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FipinqannI.class);

			subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			outTrx.setRtn_msg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private void subMainProc2(char actionType,FipinqannI inTrx,FipinqannO outTrx) {
	
		switch (actionType) {
			case 'S': {
				saveAnnouncement(inTrx,outTrx);
				break;
			}
			case 'Q': {
				setOutTrxByList(inTrx,outTrx);
				break;
			}
			default: {
				String gszMsgBuf = "Invalide type input: [" + actionType + "]";
				outTrx.setRtn_msg(gszMsgBuf);
				outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT));
			}
		}
	}

	

	public void setOutTrxByList(FipinqannI inTrx,FipinqannO outTrx){
		List<FipinqannOA> fipinqannOAList = new ArrayList<FipinqannOA>();
		String hql = "FROM Announcement where 1=1";
		List<Announcement> annList = announcementDao.find(hql);
		if(annList.size()>0){
			for(Announcement announ: annList){
				FipinqannOA fipinqannOA = new FipinqannOA();
				fipinqannOA.setAnnounce_no(announ.getAnnounce_no());
				fipinqannOA.setAnnounce_text(announ.getAnnounce_text());
				fipinqannOA.setEvt_usr(announ.getEvt_usr());
				fipinqannOA.setEvt_timestamp(announ.getEvt_timestamp());
				fipinqannOAList.add(fipinqannOA);
			}
			outTrx.setCount(annList.size());
		}else{
			outTrx.setCount(0);
		}
		outTrx.setOary(fipinqannOAList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_msg(RETURN_MESG_OK);
	}

	public void saveAnnouncement(FipinqannI inTrx,FipinqannO outTrx){
		Announcement announcement = new Announcement();

		announcement.setAnnounce_no(inTrx.getAnnounce_no());
		announcement.setEvt_usr(inTrx.getEvt_usr());
		announcement.setEvt_timestamp(Util.getCurrentTimestamp());
		announcement.setAnnounce_text(inTrx.getAnnounce_text());
		announcementDao.saveOrUpdate(announcement);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_msg(RETURN_MESG_OK);
	}

}
